package com.artisan.service.admin.impl;

import com.artisan.common.pojo.PageBean;
import com.artisan.common.pojo.PageParam;
import com.artisan.common.utils.TimeFormatUtil;
import com.artisan.dao.*;
import com.artisan.medium.admin.service.CustomerService;
import com.artisan.medium.admin.service.UserService;
import com.artisan.model.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang on 2017/7/1.
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService{
    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private AddressInfosMapper addressInfosMapper;

    @Autowired
    private CustomerMessageMapper customerMessageMapper;

    @Override
    public PageBean queryCustomer(String phone, String proId, String cityId, String areaId, Integer page, Integer pageSize) {

        List<Map<String, Object>> result = new ArrayList<>();
        Map<String, Object> paramMap = new HashMap<>();
        /*CustomerExample customerExample = new CustomerExample();
        CustomerExample.Criteria customerCriteria = customerExample.createCriteria();*/

        paramMap.put("limitStart", (page - 1)*pageSize);
        paramMap.put("pageSize", pageSize);
        if (StringUtils.isNotBlank(phone)) {
            paramMap.put("phone", phone);
        }
        if (StringUtils.isNotBlank(areaId) && !"-1".equals(areaId)) {
            paramMap.put("areaId", areaId);
        } else if (StringUtils.isNotBlank(cityId) && !"-1".equals(cityId)) {
            paramMap.put("cityId", cityId);
        } else if (StringUtils.isNotBlank(proId) && !"-1".equals(proId)) {
            paramMap.put("proId", proId);
        }

        List<Customer> list = customerMapper.queryCustomersByMap(paramMap);
        if (list != null && list.size() > 0) {
            for (Customer c : list) {
                Map<String, Object> map = new HashMap<>();
                String address = getAddress(c.getId());
                map.put("user", c);
                map.put("address", address);
                map.put("crtTime", TimeFormatUtil.dateToStrFull(c.getCrtTime()));
                result.add(map);
            }
        }

        int count = customerMapper.countByMap(paramMap);

        PageBean<List<Map<String, Object>>> bean = new PageBean<List<Map<String, Object>>>(page, pageSize, count, result);
        return bean;
    }

    /**
     *
     * @param uId
     * @return
     */
    private String getAddress(Integer uId) {
        StringBuffer sb = new StringBuffer();
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andUIdEqualTo(uId).andDefaultAddressEqualTo(true);
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        //addressMapper.selectByExample(addressExample).stream().findFirst().orElse(null);
        if (addresses != null && addresses.size() > 0) {
            Address addr = addresses.get(0);
            AddressInfos province = addressInfosMapper.selectByPrimaryKey(addr.getProvinceId());
            sb.append(province.getName());
            AddressInfos city = addressInfosMapper.selectByPrimaryKey(addr.getCityId());
            sb.append(city.getName());
            AddressInfos area = addressInfosMapper.selectByPrimaryKey(addr.getAreaId());
            sb.append(area.getName());
            if (StringUtils.isNotBlank(addr.getStreetId())) {
                AddressInfos street = addressInfosMapper.selectByPrimaryKey(addr.getStreetId());
                sb.append(street.getName());
            }
            sb.append(addr.getAddress());
        }
        return sb.toString();
    }

    /**
     * 查询用户意见反馈
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean queryCustomerSuggestBack(Integer page, Integer pageSize) {

        List<Map<String, Object>> result = new ArrayList<>();
        CustomerMessageExample example = new CustomerMessageExample();
        example.setLimitStart((page - 1)*pageSize);
        example.setPageSize(pageSize);
        example.setOrderByClause("create_time desc");
        List<CustomerMessage> list = customerMessageMapper.selectByExample(example);

        int count = customerMessageMapper.countByExample(example);
        if (list != null && list.size() > 0) {
            for (CustomerMessage cm : list) {
                Map<String, Object> map = new HashMap<>();
                Customer customer = customerMapper.selectByPrimaryKey(cm.getuId());
                map.put("message", cm.getMessage());
                map.put("crtTime", TimeFormatUtil.dateToStrFull(cm.getCreateTime()));
                map.put("logo", customer.getLogo());
                map.put("phone", customer.getPhone());
                map.put("wName", customer.getwName());
                result.add(map);
            }
        }
        return new PageBean<List<Map<String, Object>>>(page, pageSize, count, result);
    }
}
